﻿

import postgresql
import os
import sys
import postgresql.driver as pg_driver

# Настройка вывода ошибок в файл

# Определяется текущая директория

__dir__ = os.path.dirname(os.path.abspath(__file__))
path_error = os.path.join(__dir__, "translation_error.log")

# Вывод ошибок перенаправляется в файл error.log в текущей директории

sys.stderr = open(path_error, 'a')

# Определяются параметры подключения

c_user="palo"
c_password="palo"
c_host="sa-serv8"
c_port=5432
c_db="test_so"

# Выполняется подключение к базе данных с заданными параметрами

db = pg_driver.connect(user = c_user, password = c_password, host = c_host, port = c_port, database = c_db)

# Создается таблица 

#db.execute('''CREATE TABLE so_section_corr
#(
#  id serial NOT NULL,
#  s_articles_section_id integer NOT NULL,
#  s_ssections_sectname character varying(70) NOT NULL,
#  s_doclist_doc_type integer,
#  s_doctypes_doc_name character varying(100),
#  o_omp_objects_objtype integer NOT NULL,
#  o_konstrobj_itemtype integer NOT NULL,
#  o_konstrobj_attr integer NOT NULL,
#  o_business_objects_type integer NOT NULL,
#  o_businessobj_promotion_operation integer NOT NULL,
#  o_businessobj_promotion_current_state integer NOT NULL,
#  o_konstrobj_protection integer NOT NULL,
#  o_konstrobj_planningtype integer NOT NULL,
#  o_business_objects_owner integer NOT NULL,
#  o_businessobj_promotion_action integer NOT NULL,
#  o_businessobj_promotion_lastname character varying(40) NOT NULL,
#  o_businessobj_promotion_usercode integer,
#  o_businessobj_promotion_note character varying(250)
#);
#ALTER TABLE so_section_corr OWNER TO openerp;
#COMMENT ON TABLE so_section_corr IS 'Таблица соответствия разделов';
#ALTER TABLE so_section_corr
#  ADD CONSTRAINT so_section_corr_pkey PRIMARY KEY(id);
#''')






# Копируются записи из таблицы so_section в таблицу so_section_corr

#select_so_section = db.prepare("SELECT * FROM so_section")

#insert_so_section_corr = db.prepare("INSERT INTO so_section_corr(s_articles_section_id, s_ssections_sectname, o_omp_objects_objtype, o_konstrobj_itemtype, o_konstrobj_attr, o_business_objects_type, o_businessobj_promotion_operation, o_businessobj_promotion_current_state, o_konstrobj_protection, o_konstrobj_planningtype, o_business_objects_owner, o_businessobj_promotion_action, o_businessobj_promotion_lastname, o_businessobj_promotion_usercode, o_businessobj_promotion_note) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)")

#insert_so_section_corr.load_rows(select_so_section())

#В таблицу so_section_corr добавляется запись для сборочных чертежей

select_so_section = db.prepare("SELECT * FROM so_section WHERE s_articles_section_id=1")
insert_so_section_corr = db.prepare("INSERT INTO so_section_corr(s_articles_section_id, s_ssections_sectname, o_omp_objects_objtype, o_konstrobj_itemtype, o_konstrobj_attr, o_business_objects_type, o_businessobj_promotion_operation, o_businessobj_promotion_current_state, o_konstrobj_protection, o_konstrobj_planningtype, o_business_objects_owner, o_businessobj_promotion_action, o_businessobj_promotion_lastname, o_businessobj_promotion_usercode, o_businessobj_promotion_note, s_doclist_doc_type, s_doctypes_doc_name) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, 2,'Сборочный чертеж')")
insert_so_section_corr.load_rows(select_so_section())

# Закрывается подключение к базе данных

db.close()

#Вывод ошибок возвращается "в исходное положение"

sys.stderr.close()
sys.stderr = sys.__stderr__